<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>OC和C对比 | </title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../oc/README.html" />
    
    
    <link rel="prev" href="../index.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="1" data-basepath=".." data-revision="1442762455528">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Objective-C简介
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="1" data-path="occ/README.html">
            
                
                    <a href="../occ/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         OC和C对比
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="oc/README.html">
            
                
                    <a href="../oc/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         第一个OC程序
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="3.html">
            
                
                    <a href="../3.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         面向对象思想
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="4.html">
            
                
                    <a href="../4.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         类与对象
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="5.html">
            
                
                    <a href="../5.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         类的设计
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="6.html">
            
                
                    <a href="../6.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         第一个OC类
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="7.html">
            
                
                    <a href="../7.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         对象方法的声明和实现
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="8.html">
            
                
                    <a href="../8.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         类方法的声明和实现
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="9.html">
            
                
                    <a href="../9.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         OC类练习
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="10.html">
            
                
                    <a href="../10.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         对象的存储细节
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="11.html">
            
                
                    <a href="../11.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         函数与方法对比
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="12" data-path="12.html">
            
                
                    <a href="../12.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         常见错误
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="13" data-path="13.html">
            
                
                    <a href="../13.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         总结
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="14" data-path="14.html">
            
                
                    <a href="../14.html">
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                         作业
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" ></a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_61">
                    
                        <h1 id="occ">OC和C对比</h1>
<h2 id="">本小节知识点</h2>
<ol>
<li>源文件对比</li>
<li>关键字对比</li>
<li>数据类型对比</li>
<li>流程控制语句对比</li>
<li>函数(方法)定义和声明对比</li>
<li>面相对象新增特性</li>
<li>面向对象新增语法</li>
<li>新增异常处理</li>
</ol>
<hr>
<h2 id="1">1.源文件对比</h2>
<ul>
<li>C语言中常见源文件.h头文件，.c文件</li>
</ul>
<table>
<thead>
<tr>
<th>文件扩展名</th>
<th>源类型</th>
</tr>
</thead>
<tbody>
<tr>
<td>.h</td>
<td>头文件，用于存放函数声明</td>
</tr>
<tr>
<td>.c</td>
<td>C语言源文件，用于实现头文件中声明的方法</td>
</tr>
</tbody>
</table>
<ul>
<li>OC中的源文件.h头文件，.m与.mm的实现文件</li>
</ul>
<table>
<thead>
<tr>
<th>文件扩展名</th>
<th>源类型</th>
</tr>
</thead>
<tbody>
<tr>
<td>.h</td>
<td>头文件，头文件包含类、方法、属性的声明。</td>
</tr>
<tr>
<td>.m/.mm</td>
<td>类的实现文件，参与编译的文件，用来实现类中声明的方法</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="2">2.关键字对比</h2>
<ul>
<li>C语言的关键字都可以在OC源程序中使用</li>
<li>OC新增的关键字在使用时,注意部分关键字以&quot;@&quot;开头</li>
</ul>
<h2 id="imagesa1gjzdbpng"><img src="../images/a1/gjzdb.png" alt=""></h2>
<h2 id="3">3.数据类型对比</h2>
<ul>
<li>C语言数据类型</li>
</ul>
<p><img src="../images/a1/数据类型.png" alt=""></p>
<ul>
<li>OC数据类型</li>
</ul>
<p><img src="../images/a1/数据类型2.png" alt=""></p>
<table>
<thead>
<tr>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>BOOL</td>
<td>只有两个取值真和假</td>
</tr>
<tr>
<td>NSObject *</td>
<td>OC中的对象类型</td>
</tr>
<tr>
<td>id</td>
<td>动态对象类型，万能指针</td>
</tr>
<tr>
<td>SEL</td>
<td>选择器数据类型</td>
</tr>
<tr>
<td>block</td>
<td>代码块数据类型</td>
</tr>
</tbody>
</table>
<ul>
<li>OC中的类<ul>
<li>Objective-C是一种面向对象的语言,定义类是它的基本能力。</li>
<li>那什么是类呢？类是用来描述对象的，它是一系列方法和属性的集合</li>
<li>Objective-C的类声明和实现包括两个部分:接口部分和实现部分。</li>
<li>想要定义方法也就C语言中的函数，那么就必须先有类的存在</li>
</ul>
</li>
</ul>
<hr>
<h2 id="4">4.流程控制语句对比</h2>
<ul>
<li>C语言中使用的流程控制语句OC中都可以应用<pre><code class="lang-objc"><span class="hljs-keyword">if</span> 语句
<span class="hljs-keyword">switch</span>语句
<span class="hljs-keyword">while</span>语句
<span class="hljs-keyword">do</span> <span class="hljs-keyword">while</span>语句
<span class="hljs-keyword">for</span>语句
<span class="hljs-keyword">break</span>关键字
<span class="hljs-keyword">continue</span>关键字
</code></pre>
</li>
<li><p>增强for循环,用于快速迭代数组或者集合</p>
</li>
<li><p>C语言for循环</p>
<pre><code class="lang-objc"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++){
  printf(“%d”,i);
}
</code></pre>
</li>
<li>OC增强for循环<pre><code class="lang-objc"><span class="hljs-keyword">for</span> (<span class="hljs-built_in">NSString</span> *name <span class="hljs-keyword">in</span> <span class="hljs-built_in">NSArray</span>){
  <span class="hljs-built_in">NSLog</span>(@”%@”,name);
}
</code></pre>
</li>
</ul>
<hr>
<h2 id="5">5.函数(方法)定义和声明对比</h2>
<ul>
<li><p>C语言中函数的声明和实现</p>
<ul>
<li>函数声明：int sum(int a,int b);</li>
<li>函数实现：int sum(int a,int b){  return a+b; }</li>
</ul>
</li>
<li><p>OC中的方法</p>
<ul>
<li>方法声明：- (int)sum:(int) a andB:(int) b;</li>
<li>方法实现：- (int)sum:(int) a andB:(int )b{ return a+b; }</li>
</ul>
</li>
<li><p>注意: 方法只能写在类里面, 而函数可以写在任何地方</p>
<ul>
<li>对象方法，使用对象调用的方法</li>
<li>类方法，使用类名调用的方法</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">对象方法
- (<span class="hljs-keyword">id</span>)initWithString: (<span class="hljs-built_in">NSString</span> *)name;

类方法
+ (MyClass *)createMyClassWithString: (<span class="hljs-built_in">NSString</span> *)name;
</code></pre>
<hr>
<h2 id="6">6.面相对象特性</h2>
<ul>
<li>封装性</li>
<li>继承性</li>
<li>多态性
<img src="../images/a1/mxdx.png" alt=""></li>
</ul>
<hr>
<h2 id="7">7.面向对象新增语法</h2>
<ul>
<li>属性生成器<ul>
<li>@property</li>
<li>@synthesize</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">//声明属性</span>
<span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>,<span class="hljs-keyword">strong</span>)<span class="hljs-built_in">NSString</span> *name;

<span class="hljs-comment">//合成属性</span>
<span class="hljs-keyword">@synthesize</span> name = _name;
</code></pre>
<ul>
<li>分类<ul>
<li>分类与继承</li>
<li>使用分类扩展类,无需子类化</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">NSString</span> (<span class="hljs-title">MyNSString</span>)</span>

- (<span class="hljs-built_in">NSString</span> *) encryptWithMD5;

<span class="hljs-keyword">@end</span>
</code></pre>
<ul>
<li>协议<ul>
<li>使用协议声明方法</li>
<li>协议类似于C#，java中的接口</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@protocol</span> <span class="hljs-title">MyProtocol</span></span>

- (<span class="hljs-keyword">void</span>)myProtocolMethod;

<span class="hljs-keyword">@end</span>
</code></pre>
<ul>
<li>Fundation框架<ul>
<li>创建和管理集合，如数组和字典</li>
<li>访问存储在应用中的图像和其他资源</li>
<li>创建和管理字符串</li>
<li>发布和观察通知</li>
<li>创建日期和时间对象</li>
<li>操控URL流</li>
<li>异步执行代码</li>
</ul>
</li>
</ul>
<hr>
<h2 id="8">8.新增异常处理</h2>
<ul>
<li>用于处理错误信息</li>
<li><p>格式:</p>
<ul>
<li>@try .... @catch .... @finally</li>
</ul>
</li>
<li><p>示例</p>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">//创建对象car</span>
Car *car = [Car new];
<span class="hljs-keyword">@try</span> {
<span class="hljs-comment">//调用一个没有实现的方法</span>
    [car test];
}<span class="hljs-keyword">@catch</span> (<span class="hljs-built_in">NSException</span> *exception) {
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%@"</span>,exception<span class="hljs-variable">.name</span>);
}<span class="hljs-keyword">@finally</span> {
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"继续执行!\n"</span>);
}
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../index.html" class="navigation navigation-prev " aria-label="Previous page: Objective-C简介"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../oc/README.html" class="navigation navigation-next " aria-label="Next page: 第一个OC程序"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
